package com.bjx.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.bjx.auth.domain.vo.CodeVO;
import com.bjx.auth.domain.vo.LoginVO;
import com.bjx.auth.service.LoginService;
import com.bjx.common.core.domain.R;
import com.bjx.common.redis.service.RedisService;
import com.bjx.common.security.domain.LoginUserVO;
import com.bjx.common.security.util.LoginUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @desc: 登录控制器
 * @author: liushuai
 * @date: 2022/7/8 17:26
 */
@Tag(name = "LoginController",description = "登录控制器")
@RestController
public class LoginController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private LoginService loginService;


    /**
     * 用户登录
     * @param loginVO
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/7/2 15:11
     */
    @PostMapping("/login")
    @Operation(summary = "用户密码登录",tags = "LoginController")
    public R login(@RequestBody @Validated LoginVO loginVO){
        LoginUserVO loginUserVO = loginService.login(loginVO);
        return R.success(loginUserVO);
    }

    /**
     * 获取当前登录的用户信息
     * @param
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/7/2 15:12
     */
    @GetMapping("/selectLoginUserInfo")
    @Operation(summary = "获取用户信息")
    public R<LoginUserVO> selectLoginUserInfo(){
        LoginUserVO loginUserVO = LoginUtil.loginInfo();
        return R.success(loginUserVO,"获取成功");
    }

    /**
     * 获取验证码
     * @param response
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/7/2 15:13
     */
    @GetMapping("/getCode")
    @Operation(summary = "获取验证码")
    public R<CodeVO> getCode(HttpServletResponse response){
        CodeVO code = loginService.getCode(response);
        return R.success(code);
    }

    /**
     * 注销当前登录用户
     * @param
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/7/15 15:03
     */
    @GetMapping("/logout")
    @Operation(summary = "用户注销")
    public R logout(){
        StpUtil.logout();
        return R.success();
    }


}
